/*
 * Copyright (C) 2010 CODEBISTRO LLC, Sunnyvale, California.
 * All rights reserved. Proprietary and Confidential.
 */

package org.codebistro.util;

import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Platform-specific definitions
 */
public class Platform {
	private static final Logger log = LoggerFactory.getLogger(Platform.class);

	public static final String JAVA_PROP_OS_NAME = "os.name";
	public static final String JAVA_PROP_USER_HOME = "user.home";

	public static boolean isMacOS() {
		return getOsName().contains("mac os");
	}

	public static boolean isWindows() {
		return getOsName().startsWith("windows");
	}

	public static boolean isWin9X() {
		return getOsName().indexOf("9") > -1;
	}

	public static boolean isUnix() {
		final String os = getOsName();

		return os.indexOf("linux") >= 0 || os.indexOf("unix") >= 0 || os.indexOf("redhat") >= 0;
	}

	public static boolean isVista() {
		return getOsName().indexOf("vista") >= 0;
	}

	/**
	 * Set the name of the application, as seen in the OS shell (e.g. Mac OS X Finder)
	 * @param name to set for the app
	 */
	public static void setApplicationName(String name) {
		if (isMacOS()) {
			System.setProperty("com.apple.mrj.application.apple.menu.about.name", name);
		}
	}

	public static String getUserHome() {
		return getProp(JAVA_PROP_USER_HOME, true, true);
	}

	public static String getProp(final String propKey) {
		return getProp(propKey, true, true);
	}

	/**
	 * @return lower case of os name property
	 */
	public static String getOsName() {
		return getProp(JAVA_PROP_OS_NAME, true, true).toLowerCase();
	}

	public static String getProp(final String propKey, final boolean failOnError, final boolean failOnEmpty) {
		try {
			final String propValue = System.getProperty(propKey);

			if (Strings.isNullOrEmpty(propValue)) {
				final String message = "Java runtime property '" + propKey + "' should be set to non-empty value";
				log.warn(message);
				if (failOnEmpty) {
					throw new Bark(message);
				}
			}

			return propValue;
		} catch (SecurityException e) {
			final String message = "Reading java runtime property '" + propKey + "' not permitted: " + Bark.report(e);
			log.warn(message);

			if (!failOnError) {
				return null;
			}

			throw new Bark(message);
		}
	}

	public static String getEnv(final String propName) {
		return getEnv(propName, true, true);
	}

	public static String getEnv(final String propKey, final boolean failOnError, final boolean failOnEmpty) {
		try {
			final String propValue = System.getenv(propKey);

			if (Shortcut14.isEmpty(propValue)) {
				final String message = "Environment property '" + propKey + "' should be set to non-empty value";
				log.warn(message);
				if (failOnEmpty) {
					throw new Bark(message);
				}
			}

			return propValue;
		} catch (SecurityException e) {
			final String message = "Reading environment property '" + propKey + "' not permitted: " + Bark.report(e);
			log.warn(message);

			if (!failOnError) {
				return null;
			}

			throw new Bark(message);
		}
	}
}
